package kr.co.doriclassic.chapter16;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;

import javax.swing.JButton;

import kr.co.doriclassic.chapter15.MyPanel;


class LightPanel extends JPanel implements ActionListener{
	boolean flag = false;
	private int light_number=0;
	
	
	public LightPanel() {
		setLayout(new BorderLayout());
		JButton btnLight = new JButton("trafic light turn on");
		btnLight.addActionListener(this);
		add(btnLight, BorderLayout.SOUTH);		
	}	
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		int[] x = {150,100,200};
		int[] y = {250,350,350};
		g.setColor(Color.BLACK);
		g.drawRect(100, 50, 100, 100);
		g.drawOval(100, 150, 100, 100);
		g.drawPolygon(x,y,x.length);		
		
		if(light_number == 0){
			g.setColor(Color.RED);
			g.fillRect(100, 50, 100, 100);
		}
		else if(light_number == 1){
			g.setColor(Color.GREEN);
			g.fillOval(100, 150, 100, 100);
		}
		else if(light_number == 2){
			g.setColor(Color.YELLOW);
			g.fillPolygon(x,y,x.length);
		}
	}
	

	@Override
	public void actionPerformed(ActionEvent e) {
		if(++light_number>=3){
			light_number=0;
		}
		repaint();
	}
	
}

public class PaintComponentEx extends JFrame {
	public PaintComponentEx() {
		add(new LightPanel());
		setSize(300,450);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new PaintComponentEx();
	}
}
